package com.formina.stars;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MotionEvent;

import com.formina.engine.GameActivity;

public class ObjreaderActivity extends GameActivity {

	//private StarGameView gameView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.gameView = new StarGameView(this);
        setContentView(gameView);
        this.mContext = this;
        
        SharedPreferences settings = getSharedPreferences(Preferences.PREFS,0);
        boolean random = settings.getBoolean(Preferences.ISRANDOM, true);
        int re = settings.getInt(Preferences.RED, 1);
        int bl = settings.getInt(Preferences.BLUE, 1);
        int gr = settings.getInt(Preferences.GREEN, 1);
        
        ((StarGameView)gameView).SetColor(re, bl, gr,random);	
        
	}
    
    
	@Override
   	public boolean onTouchEvent(MotionEvent event) {
   		
        

		return false;
		
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
	    super.onCreateOptionsMenu(menu);
	    createMenu(menu);
	    return true;
	}

	
	private int red,blue,green;
	private boolean random;
	private Context mContext;
	
	public static final int PREFERENCES_SAVED = 1302011;
	private void createMenu(Menu menu){
	    android.view.MenuItem v = menu.add("Preferences");
	    {
	        v.setAlphabeticShortcut('p');
	        v.setIcon(android.R.drawable.ic_menu_preferences);
	        v.setOnMenuItemClickListener(new OnMenuItemClickListener(){

				@Override
				public boolean onMenuItemClick(MenuItem arg0) {
					// TODO Auto-generated method stub
					
					
					Intent i = new Intent(mContext,Preferences.class);
					startActivityForResult(i, PREFERENCES_SAVED);
					
					return true;
					
				}
	        	
	        });
	        //v.setShowAsAction(android.view.MenuItem.);
	    }
	}
	
	@Override 
	public void onActivityResult(int requestCode, int resultCode, Intent data) {     
	  super.onActivityResult(requestCode, resultCode, data); 
	  switch(requestCode) { 
	    case (PREFERENCES_SAVED) : { 
	      if (resultCode == Activity.RESULT_OK) { 
	    	  
	    	  red = data.getIntExtra(Preferences.RED, 0);
	    	  blue = data.getIntExtra(Preferences.BLUE, 0);
	    	  green = data.getIntExtra(Preferences.GREEN, 0);
	    	  random = data.getBooleanExtra(Preferences.ISRANDOM, true);
	    	((StarGameView)gameView).SetColor(red, blue, green,random);	      // TODO Switch tabs using the index.
	    	
	      } 
	      break; 
	    } 
	  } 
	}
	
}